from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.
from django.utils.decorators import method_decorator
from django.views import View


def student(request):
    # get
    if request.method == 'GET':
        print('处理了get请求')
        return HttpResponse('获得一个学生的信息')

    # post
    if request.method == 'POST':
        print('处理了post请求')
        return HttpResponse('新增一个学生的信息')

    # put
    if request.method == 'PUT':
        print('处理了put请求')
        return HttpResponse('修改一个学生的信息')

    # delete
    if request.method == 'DELETE':
        print('处理了delete请求')
        return HttpResponse('删除了一个学生的信息')


def teacher(request):
    # get
    if request.method == 'GET':
        print('处理了get请求')
        return HttpResponse('获得一个老师的信息')

    # post
    if request.method == 'POST':
        print('处理了post请求')
        return HttpResponse('新增一个老师的信息')

    # put
    if request.method == 'PUT':
        print('处理了put请求')
        return HttpResponse('修改一个老师的信息')

    # delete
    if request.method == 'DELETE':
        print('处理了delete请求')
        return HttpResponse('删除了一个老师的信息')


from django.views import View

def my_decorator(func):
    def wrapper(request, *args, **kwargs):
        print('自定义装饰器被调用了')
        print('请求路径%s' % request.path)
        return func(request, *args, **kwargs)
    return wrapper

class PeopleView(View):
    # delete 用来删除用户
    def delete(self, request):
        print('处理了delete请求')
        return HttpResponse('删除了用户')


# 定义类视图
# 把my_decorator 装饰在 get方法上
#@method_decorator(my_decorator, name='get')
class StudentView(PeopleView, View):
    # 定义方法 处理GET请求,   方法名字就必须是 get
    # @method_decorator(my_decorator)
    def get(self, request):

        print('处理了get请求')
        return HttpResponse('获得一个学生的信息')

    # 定义方法 处理POST请求, 方法名字必须是 post
    @method_decorator(my_decorator)
    def post(self, request):
        print('处理了post请求')
        return HttpResponse('新增一个学生的信息')

    # put
    def put(self, request):
        print('处理了put请求')
        return HttpResponse('修改一个学生的信息')

    # delete
    # def delete(self, request):
    #     print('处理了delete请求')
    #     return HttpResponse('删除了一个学生的信息')


# 定义类视图
class TeacherView(PeopleView, View):
    # 定义方法 处理GET请求,   方法名字就必须是 get
    def get(self, request):
        print('处理了get请求')
        return HttpResponse('获得一个老师的信息')

    # 定义方法 处理POST请求, 方法名字必须是 post
    def post(self, request):
        print('处理了post请求')
        return HttpResponse('新增一个老师的信息')

    # put
    def put(self, request):
        print('处理了put请求')
        return HttpResponse('修改一个老师的信息')

    # delete
    # def delete(self, request):
    #     print('处理了delete请求')
    #     return HttpResponse('删除了一个老师的信息')
